Ext.define('AgenteNET.controller.utilitarios.Funcoes', {
	extend : 'Ext.app.Controller',
	stores:['utilitarios.Funcoes'],
	views:['utilitarios.funcoes.Listar','utilitarios.funcoes.Novo','utilitarios.funcoes.Editar'],
	init : function() {
		this.control({
			'utilitariosFuncoesListarView treepanel' : {
				selectionchange:function(item,model){
					var grid=item.view.up('treepanel').up('panel').down('grid');
					grid.store.clearFilter();
					if(model[0].get('id')!=1)grid.store.filter('primario',model[0].get('id'));
					else grid.store.filter();
				}
			},
			'utilitariosFuncoesNovoView textfield[name="video"]' : {
				render:function(el){
					el.up('panel').down('panel[cls="videoPlayer"]').removeAll(true);
					el.up('panel').down('panel[cls="videoPlayer"]').add({
						html:  '<iframe width="255" height="180" src="http://www.youtube.com/embed/'+el.getValue()+'?version=2&fs=1&autoplay=1" frameborder="0" allowfullscreen></iframe>',
					});
				},
				change:function(el){
					el.up('panel').down('panel[cls="videoPlayer"]').removeAll(true);
					el.up('panel').down('panel[cls="videoPlayer"]').add({
						html:  '<iframe width="255" height="180" src="http://www.youtube.com/embed/'+el.getValue()+'?version=2&fs=1&autoplay=1" frameborder="0" allowfullscreen></iframe>',
					});
				},
			},
			'utilitariosFuncoesEditarView textfield[name="video"]' : {
				render:function(el){
					el.up('panel').down('panel[cls="videoPlayer"]').removeAll(true);
					el.up('panel').down('panel[cls="videoPlayer"]').add({
						html:  '<iframe width="255" height="180" src="http://www.youtube.com/embed/'+el.getValue()+'?version=2&fs=1&autoplay=1" frameborder="0" allowfullscreen></iframe>',
					});
				},
				change:function(el){
					el.up('panel').down('panel[cls="videoPlayer"]').removeAll(true);
					el.up('panel').down('panel[cls="videoPlayer"]').add({
						html:  '<iframe width="255" height="180" src="http://www.youtube.com/embed/'+el.getValue()+'?version=2&fs=1&autoplay=1" frameborder="0" allowfullscreen></iframe>',
					});
				},
			},
		});
		this.inicializado = true;
	},
	Listar:function(el){
		return Ext.create('AgenteNET.view.utilitarios.funcoes.Listar');
	},
	Novo : function(el) {
		var form=Ext.create('AgenteNET.view.utilitarios.funcoes.Novo');
		form.loadRecord(this.getStore('utilitarios.Funcoes').model.create({}));
		return form;
	},
	Editar : function(el,model) {
		var form=Ext.create('AgenteNET.view.utilitarios.funcoes.Editar');
		form.loadRecord(model);
		return form;
	},
	Adicionar: function(el){
		var form=el.up('form'),win=el.up('window'),model=form.getRecord();
		if(!form.getForm().isValid())return false;
		model.set(form.getValues());
		
		var ope={
		    win:win,
		    elform:form,
		    model:model,
			success:function(el,op){
				var b;if(op.win)if(b=op.win.down('button[text="Fechar"]'))b.fireEvent('click',b);
		    	if(op.elform.up('panel[chamadoPor]'))
		    		if(op.elform.up('panel[chamadoPor]').chamadoPor){
		    			if(op.elform.up('panel[chamadoPor]').chamadoPor.store){
							b=op.elform.up('panel[chamadoPor]').chamadoPor.store;
							b.load(); //fireEvent('load',b);
						}
		    			if(op.elform.up('panel[chamadoPor]').chamadoPor.up('panel'))
			    			if(op.elform.up('panel[chamadoPor]').chamadoPor.up('panel').down('treepanel'))
				    			if(op.elform.up('panel[chamadoPor]').chamadoPor.up('panel').down('treepanel').store){
									b=op.elform.up('panel[chamadoPor]').chamadoPor.up('panel').down('treepanel').store;
									b.load();
				    			}
					}
			},
		    failure: function() {
			       console.log(arguments);
		    },
		}

		if(!form.getForm().hasUpload())
			model.save(ope);
		else{
			ope.clientValidation=true;
			ope.url=model.getProxy().url;
			ope.method='POST';
			ope.params={_method:'post'};
			form.submit(ope);
		}
		return model;
	},
	Salvar: function(el){
		var form=el.up('form'),win=el.up('window'),model=form.getRecord();
		if(!form.getForm().isValid())return false;
		model.set(form.getValues());
		
		var ope={
		    win:win,
		    elform:form,
		    model:model,
			success:function(el,op){
				var b;if(op.win)if(b=op.win.down('button[text="Fechar"]'))b.fireEvent('click',b);
		    	if(op.elform.up('panel[chamadoPor]'))
		    		if(op.elform.up('panel[chamadoPor]').chamadoPor){
		    			if(op.elform.up('panel[chamadoPor]').chamadoPor.store){
							b=op.elform.up('panel[chamadoPor]').chamadoPor.store;
							b.load(); //fireEvent('load',b);
						}
		    			if(op.elform.up('panel[chamadoPor]').chamadoPor.up('panel'))
			    			if(op.elform.up('panel[chamadoPor]').chamadoPor.up('panel').down('treepanel'))
				    			if(op.elform.up('panel[chamadoPor]').chamadoPor.up('panel').down('treepanel').store){
									b=op.elform.up('panel[chamadoPor]').chamadoPor.up('panel').down('treepanel').store;
									b.load();
				    			}
					}
			},
		    failure: function() {
			       console.log(arguments);
		    },
		}

		if(!form.getForm().hasUpload())
			model.save(ope);
		else{
			ope.clientValidation=true;
			ope.url=model.getProxy().url+'/id/'+model.get('id');
			ope.method='POST';
			ope.params={_method:'put'};
			form.submit(ope);
		}
		return model;
	},
	Excluir: function(el){
		var win=null,form=null,records=[],storeGrid=null,storeTree=null;
		if(el.up('form')){
			form=el.up('form');
			win=el.up('window');
			records[0]=el.up('form').getRecord();
		}else if(el.up('grid')){
			console.log(el.up('grid').up('panel'));
			records=el.up('grid').getSelectionModel().getSelection();
			storeGrid=el.up('grid').store;
			storeTree=el.up('grid').up('panel').down('treepanel').store;
		}
		if(records.length==0){
			Ext.MessageBox.show({
		           title: 'Exclusão de Ações',
		           msg: 'Nenhuma registro de ação foi selecionada para exclusão',
		           buttons: Ext.MessageBox.OK,
		           animateTarget: el,
		           icon: Ext.MessageBox.ERROR
			});
		}
		if(records.length>0){
			Ext.MessageBox.confirm({
		           title: 'Exclusão de ações',
		           msg:'Confirma a'+(records.length>1?'s':'')+' exclus'+(records.length>1?'ões':'ão')+' da'+(records.length>1?'s':'')+' aç'+(records.length>1?'ões':'ão')+' '+(records.length>1?'selecionadas':''),
		           icon: Ext.MessageBox.QUESTION,
		           buttons: Ext.MessageBox.YESNO,
 		           animateTarget: el,
		            buttonText:{ 
		                yes: "Sim", 
		                no	: "Não" 
		            },
		            scope:this,
		            fn: function(resposta){
		            	if(resposta=='yes'){
	            			if(storeGrid){
	            				storeGrid.clearFilter();
		            			storeGrid.remove(records);
		            			storeGrid.sync();
		            			/**
		            			 * Todo: Fazer o load apos ter excluido no banco de dados
		            			 */
		            			storeTree.load();
		            		}else{
		            			form.submit({
		            			    clientValidation: true,
		            			    url: records[0].getProxy().url+'/id/'+records[0].get('id'),
		            			    method:'POST',
		            			    params:{_method:'delete'},
		            			    win:win,
		            			    elform:form,
		            			    success: function(form, op) {
		            					var b;if(op.win)if(b=op.win.down('button[text="Fechar"]'))b.fireEvent('click',b);
		            			    	if(op.elform.up('panel[chamadoPor]'))
		            			    		if(op.elform.up('panel[chamadoPor]').chamadoPor){
		            			    			if(op.elform.up('panel[chamadoPor]').chamadoPor.store){
		            								b=op.elform.up('panel[chamadoPor]').chamadoPor.store;
		            								b.load(); //fireEvent('load',b);
		            							}
		            			    			if(op.elform.up('panel[chamadoPor]').chamadoPor.up('panel'))
		            				    			if(op.elform.up('panel[chamadoPor]').chamadoPor.up('panel').down('treepanel'))
		            					    			if(op.elform.up('panel[chamadoPor]').chamadoPor.up('panel').down('treepanel').store){
		            										b=op.elform.up('panel[chamadoPor]').chamadoPor.up('panel').down('treepanel').store;
		            										b.load();
		            					    			}
		            						}
		            			    },
		            			    failure: function(form, action) {
		            				       console.log(arguments);
		            			    }
		            			});
		            		}
		            	}
		            }
			});
		}
		return records;
	},
});
